library pp_group;

import 'package:flutter/material.dart';
import 'package:pp_uikit/pp_card.dart';
import 'package:pp_uikit/pp_line.dart';
import 'package:pp_uikit/theme/pp_group_theme.dart';

export 'package:pp_uikit/pp_group/pp_group_block_input.dart';
export 'package:pp_uikit/pp_group/pp_group_custom.dart';
export 'package:pp_uikit/pp_group/pp_group_input.dart';
export 'package:pp_uikit/pp_group/pp_group_switch.dart';
export 'package:pp_uikit/pp_group/pp_group_title.dart';
export 'package:pp_uikit/theme/pp_group_theme.dart';

///
/// 可展开收起组类型录入项
/// 内部可包含其他类型Widget
///
/// 包括"标题"、"副标题"、"错误信息提示"、"必填项提示"、"添加/删除按钮"、"消息提示"
///
// ignore: must_be_immutable
class PPGroup extends StatefulWidget {
  /// 录入项的唯一标识，主要用于录入类型页面框架中
  final String? label;

  ///外边距 默认是0
  final EdgeInsetsGeometry margin;

  ///内间距 默认是0
  final EdgeInsetsGeometry padding;

  /// 录入项标题
  final String title;

  /// 录入项子标题
  final String? subTitle;

  /// 录入项提示（问号图标&文案） 用户点击时触发onTip回调。
  /// 1. 若赋值为 空字符串（""）时仅展示"问号"图标，
  /// 2. 若赋值为非空字符串时 展示"问号图标&文案"，
  /// 3. 若不赋值或赋值为null时 不显示提示项
  /// 默认值为 3
  final String? tipLabel;

  /// 录入项错误提示
  final String error;

  /// 录入项是否为必填项（展示*图标） 默认为 false 不必填
  final bool isRequire;

  /// 是否添加分割线
  final bool addLine;

  /// item间的间距
  final double separatorHeight;

  /// 录入项 是否可编辑
  final bool isEdit;

  /// 点击"-"图标回调
  final VoidCallback? onRemoveTap;

  /// 点击"？"图标回调
  final VoidCallback? onTip;

  /// 右侧文案
  final String? deleteLabel;

  /// 内部子项
  final List<Widget> children;

  final CrossAxisAlignment widgetAlignment;

  final double radius;

  /// form配置
  PPGroupTheme? themeData;

  PPGroup({
    Key? key,
    this.label,
    this.title = "",
    this.subTitle,
    this.tipLabel,
    this.error = "",
    this.isEdit = true,
    this.isRequire = false,
    this.addLine = true,
    this.separatorHeight = 0.0,
    this.radius = 10,
    this.onRemoveTap,
    this.onTip,
    this.deleteLabel,
    this.margin = const EdgeInsets.only(top: 12, left: 12, right: 12),
    this.padding = const EdgeInsets.only(left: 12, right: 10),
    this.widgetAlignment = CrossAxisAlignment.start,
    required this.children,
  }) : super(key: key) {
    themeData ??= PPGroupTheme();
  }

  @override
  PPGroupState createState() {
    return PPGroupState();
  }
}

class PPGroupState extends State<PPGroup> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        PPCard(
          padding: widget.padding,
          margin: widget.margin,
          circular: widget.radius,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (widget.title.isNotEmpty)
                Row(
                  children: <Widget>[
                    Container(
                      margin: const EdgeInsets.only(top: 12, bottom: 6),
                      child: Text(
                        widget.title,
                        style: widget.themeData?.headTitleTextStyle,
                      ),
                    ),
                  ],
                ),
              Column(
                crossAxisAlignment: widget.widgetAlignment,
                children: getSubItem(),
              ),
            ],
          ),
        )
      ],
    );
  }

  List<Widget> getSubItem() {
    List<Widget> result = <Widget>[];

    if (widget.children.isEmpty) {
      return result;
    }

    for (var i = 0; i < widget.children.length; i++) {
      Widget w = widget.children[i];
      if (widget.addLine && i != 0) {
        result.add(const PPLine());
      }
      if (widget.separatorHeight > 0) {
        result.add(SizedBox(height: widget.separatorHeight));
      }
      result.add(w);
    }

    return result;
  }
}
